package com.example.music.fragment;

import static android.content.ContentValues.TAG;

import android.app.AlertDialog;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.music.R;
import com.example.music.adapter.PlaySongListAdapter;
import com.example.music.db.PlaylistDAO;
import com.example.music.model.Song;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PlaylistFragment extends Fragment {
    private RecyclerView recyclerView;
    private PlaySongListAdapter adapter;
    private List<PlaylistDAO.Playlist> playlists = new ArrayList<>();
    private PlaylistDAO playlistDao;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_play_song_lists, container, false);
        recyclerView = view.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        playlistDao = new PlaylistDAO(getContext());
        loadPlaylists();

        FloatingActionButton fab = view.findViewById(R.id.fab_add_playlist);
        fab.setOnClickListener(v -> showCreatePlaylistDialog());

        return view;
    }

    private void loadPlaylists() {
        playlists.clear(); // 清空现有播放列表
        playlists.addAll(playlistDao.getAllPlaylists());
        if (adapter == null) {
            adapter = new PlaySongListAdapter(getContext(), playlists, playlistDao);
            recyclerView.setAdapter(adapter);
        } else {
            adapter.updateData(playlists); // 更新适配器数据
        }
    }

    private void showCreatePlaylistDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setTitle("新建歌单");

        final EditText input = new EditText(getContext());
        builder.setView(input);

        builder.setPositiveButton("创建", (dialog, which) -> {
            String playlistName = input.getText().toString().trim();
            if (!playlistName.isEmpty()) {
                long playlistId = playlistDao.createPlaylist(playlistName);
                if (playlistId != -1) {
                    PlaylistDAO.Playlist playlist = new PlaylistDAO.Playlist();
                    playlist.setId(playlistId);
                    playlist.setName(playlistName);
                    playlist.setCreateTime(System.currentTimeMillis());

                    playlists.add(0, playlist);
                    adapter.notifyItemInserted(0);
                    recyclerView.smoothScrollToPosition(0);
                }
            }
        });

        builder.setNegativeButton("取消", null);
        builder.show();
    }

    // 添加 refreshData 方法
    public void refreshData() {
        loadPlaylists();
    }
}



